package te;

import joptsimple.internal.Strings;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author tzp
 * @since 2020/12/17
 */
public class BigIntTest {


    public static final int SL = 8;
    public static final int SLE = 100000000;

    public static String bigSum(String a, String b) {
        int indexA = a.length();
        int indexB = b.length();
        int extra = 0;
        List<String> segments = new LinkedList<>();
        int x = 0;
        while (indexA > 0 || indexB > 0) {
            String tmpa = a.substring(Math.max(indexA - SL, 0), indexA);
            String tmpb = b.substring(Math.max(indexB - SL, 0), indexB);
            tmpa = tmpa.length() > 0 ? tmpa : "0";
            tmpb = tmpb.length() > 0 ? tmpb : "0";
            int sum = Integer.parseInt(tmpa) + Integer.parseInt(tmpb) + extra;
            if (sum >= SLE) {
                extra = 1;
                sum = sum % SLE;
            }

            String r = String.valueOf(sum);
            x = SL - r.length();
            r = Strings.repeat('0', x) + r;
            segments.add(0, r);

            indexA = Math.max(indexA - SL, 0);
            indexB = Math.max(indexB - SL, 0);
        }


        return String.join("", segments).substring(x);
    }

    public static void main(String[] args) {
        System.out.println(1211312312 + 121123123);
        System.out.println(bigSum("1211312312", "121123123"));
        System.out.println(bigSum("99999999999", "1"));
        System.out.println(bigSum("99999999999", "99999999999"));
        System.out.println(bigSum("11111111121132131231211111", "9999999932112312312312999"));
    }
}
